//
//  ASImagesCellSupport.swift
//  SmoSky
//
//  Created by zhu xietong on 2018/5/21.
//  Copyright © 2018年 tianrun. All rights reserved.
//

import Foundation
import Eelay
import AsyncDisplayKit
import JXPhotoBrowser


public class CommentImgKitDemo: ASScanSupportKit {
    
}
public class CommentCellDemo: ASImagesScanCell<CommentImgKitDemo> {
    
    override public func addLayoutRules() {
    
        super.addLayoutRules()
    }
    
    override public func mo_model_reload() {
        let list = [
            ["img":__images.one_str],
            ["img":__images.one_str],
            ["img":__images.one_str],
            ["img":__images.one_str],
            ["img":__images.one_str],
            ["img":__images.one_str],
            ]
        imgsView.mo.model = [
            "list":list
            ].mutable_dictionary
        
        var models = [EeImageResourceModel]()
        
        for one in list{
            let newm = EeImageResourceModel()
            newm.firstLevelUrl = one["img"]!
            newm.secondLevelUrl = one["img"]!
            newm.thirdLevelUrl = one["img"]!
            models.append(newm)
            
        }
        
    }
    
    override public func specGetter() -> _Spec {
        let spec:_Spec = .inset([], .zero, child: imgsView[Swidth])
        
        return spec
    }
}


open class ASScanSupportKit: JoTouchNode {
    
    
    public var imageNode = JoImageNode()
    
    open var itemSize:CGSize{
        get{
            return CGSize(width: 50, height: 50)
        }
    }
    
    open var space:(X:CGFloat,Y:CGFloat){
        get{
            return (8,8)
        }
    }
    
    
    open override func specGetter() -> _Spec {
        let size:CGSize = itemSize
        
        let inset:_Spec = .inset([], [4,0,4,8], child: imageNode["\(size.height)",size.width])
        return inset
    }
    
    
    
    override open func mo_model_reload() {
        
        
        let img_url = mo.model["img",""]
        imageNode.img_url = img_url
        imageNode.view.backgroundColor = UIColor.table_bg_color
    }
    
}


open class ASImagesScanCell<Kit:ASScanSupportKit>:ASSizeRangeCell,EeActionDelegate{
    
    public let imgsView = JoRuleNode<Kit>()
    
    public var browerModels:[EeImageResourceModel] = [EeImageResourceModel]()

    
    open func active(onwer: Any, actionID: String, data: NSMutableDictionary) {
        if let kit = onwer as? Kit
        {
            let index = data[int:"__index",0]
            self.openPhotoBrowserWithInstanceMethod(openIndex: index, imageView: kit.imageNode.imageView)
            
            //            showImg(view: t)
        }
    }
    
    open override func addLayoutRules() {
        imgsView.delegate = self
        imgsView.space = 0
    }
    
    open func openPhotoBrowserWithInstanceMethod(openIndex:Int,imageView:UIImageView?){
        let models = browerModels
        // 网图加载器，WebP加载器
//        let loader = JXKingfisherLoader()
        
        
        let browser = JXPhotoBrowser()
        browser.numberOfItems = {
            models.count
        }
        browser.reloadCellAtIndex = { context in
            let url = models[context.index].secondLevelUrl.flatMap { URL(string: $0) }
            let browserCell = context.cell as? JXPhotoBrowserImageCell
//
//            let collectionPath = IndexPath(item: context.index, section: indexPath.section)
//            let collectionCell = collectionView.cellForItem(at: collectionPath) as? BaseCollectionViewCell
//            let placeholder = collectionCell?.imageView.image
//            // 用Kingfisher加载
            let placeholder = imageView?.image
            browserCell?.imageView.kf.setImage(with: url, placeholder: placeholder, options: [], completionHandler: { _ in
                browserCell?.setNeedsLayout()
            })
        }
        let kits = self.imgsView.nodes
        browser.transitionAnimator = JXPhotoBrowserZoomAnimator(previousView: { index -> UIView? in
            let k = kits[index]
            
            return k.imageNode.imageView
        })
        browser.pageIndex = openIndex
        browser.show()
        
//        let loader = JXPhotoBrowser.KingfisherLoader()
//
//        // 数据源
//
//        let dataSource = JXRawImageDataSource(photoLoader: loader, numberOfItems: { () -> Int in
//            return models.count
//        }, placeholder: { index -> UIImage? in
//            return imageView?.image
//        }, autoloadURLString: { index -> String? in
//            return models[index].secondLevelUrl
//        }) { index -> String? in
//            return models[index].thirdLevelUrl
//        }
//        // 视图代理，实现了光点型页码指示器
//        let delegate = JXDefaultPageControlDelegate()
//        // 转场动画
//        let kits = self.imgsView.nodes
//        let trans = JXPhotoBrowserZoomTransitioning { (browser, index, view) -> UIView? in
//            let k = kits[index]
//
//            return k.imageNode.imageView
//
////            return nil
//        }
//        // 打开浏览器
//        JXPhotoBrowser(dataSource: dataSource, delegate: delegate, transDelegate: trans)
//            .show(pageIndex: openIndex)
    }

        
}



